package com.lili.goods.controller;

import com.lili.common.result.Result;
import com.lili.goods.service.GoodsCategoryService;
import com.lili.model.goods.GoodsCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods/category")
@Api("商品分类管理接口")
public class GoodsCategoryController {

    @Autowired
    GoodsCategoryService goodsCategoryService;

    /**
     * 保存商品分类
     * @param goodsCategory
     * @return
     */
    @ApiOperation(value = "保存商品")
    @PostMapping("/save")
    public Result saveCategory(@RequestBody GoodsCategory goodsCategory){
        int rs = goodsCategoryService.saveCategory(goodsCategory);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 查询所有的商品分类
     * @return
     */
    @GetMapping("/query/tree")
    public Result queryCategoryTree(){
        List<Map> list = goodsCategoryService.queryCategoryTree();
        return Result.ok(list);
    }

    /**
     * 修改商品分类的禁用状态
     * @param map
     * @return
     */
    @PutMapping("/modify/status")
    public Result modifyCategoryStatus(@RequestBody Map map){
        Long id = Long.parseLong(map.get("id").toString());
        int status = Integer.parseInt(map.get("status").toString());
        int rs = goodsCategoryService.modifyStatus(id,status);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 更新商品分类的信息
     * @param goodsCategory
     * @return
     */
    @PutMapping("/update/category")
    public Result updateCategoryInfo(@RequestBody GoodsCategory goodsCategory){
        int rs = goodsCategoryService.updateCategory(goodsCategory);
        return rs>=1?Result.ok():Result.fail();
    }

    /**
     * 删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/category")
    public Result deleteCategory(@RequestParam Long id){
        int rs = goodsCategoryService.deleteCategory(id);
        return rs==1?Result.ok():Result.fail();
    }

}
